/*
 * =====================================================================================
 *
 *       Filename:  test.cpp
 *
 *    Description:  Testing simulated annealing
 *
 *        Version:  1.0
 *        Created:  06/07/12 14:19:14
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Moos Hueting (mh), mooshueting@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <iostream>
#include <fstream>

#include "sa.h"
#include "features/char_features.h"

#define N_RUNS 2000

using namespace std;

int main(int argc, char* argv[]) {

//    Mat image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
//    threshold(image, image, 100, 255, THRESH_BINARY);

    // initialize simulated annealing with provided CSV file
    SA sa(argv[1]);

    // initialize file for writing of temperature and energy
    ofstream f;

    // check if we will write to file
    if (argc > 2)
        f.open(argv[2]);

    // execute runs
    for (int i = 0; i < N_RUNS; i++) {
        // start lowering temperature only when some pixels are not black anymore
        if (!(i % 1) && sa.energy() > -1e+11)
            sa.lowerTemperature();
        sa.step();
        printf("%.2f                     \n", sa.energy());

        float e = sa.energy();
        if (argc > 1 && e > -1e+11 )
            f << e << ", " << sa.getTemperature() << "\n";

        Mat strel = getStructuringElement(MORPH_ELLIPSE, Size(3,3));

        Mat disp;
        dilate(sa.getIso(), disp, strel, Point(-1,-1), 3);

        imshow("Display", disp);
        waitKey(1);
    }

    if (argc > 2)
        f.close();

    //cout << sa.energy() << endl;

    Mat disp;
    sa.getIso().convertTo(disp, CV_8UC1);
    cout << "corners: " << corners(disp) << endl;
    cout << "convexity: " << convexity(disp) << endl;
    cout << "complexity: " << complexity(disp) << endl;
    cout << "diffsegs0: " << diffsegs(disp,0) << endl;
    cout << "diffsegs1: " << diffsegs(disp,1) << endl;
    cout << "ratio: " << ratio(disp) << endl;
    cout << "junctions: " << junctions(disp) << endl;
    cout << "density: " << density(disp) << endl;
    disp *= 255;

    while(1) {
        imshow("Display", disp);
        waitKey(1);
    }


}
